package com.hjm.common.api.cq;

import cn.hutool.core.map.MapUtil;
import com.alibaba.fastjson.JSONObject;
import com.hjm.common.utils.DateUtils;
import com.hjm.common.utils.LogUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName PublicRequest
 * @Deacription 公共的请求上游类
 * @Author Gd
 * @Version 1.0
 **/
public class PublicJgRequest {


    public static final String AGENT_NO_JG = "100030783";
    public static final String PUBLIC_JG = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAoaw6xt9jXehpxOa_h35n3C6iqFiE8P4SZZraj-G5xsM1uvwAqnyZSRNta6cs89LlM5B6bYuR-46rwXxoXfYB9HOj6BSwELVX_TONmaGDNAz_5DeK5K5vheVER8yulYZdHLtmnbzGYshZDg_cO8NO1JecstMsazohseHO4qG6yaHzKENrgnPu53bJaKuTZ8SqiTP_LwwDuukeQ-boLfyc6qcmKpv1sdT1Z-EBlxn7AiNhheFq1hIem98X--1jtvB9gMFgsHhRBJ1nVvJA0H-JrrV5fmEKzJCP4y1TG-PIJroqzv8BicW7DexUIwE0nK3xzblxV0RYo8TOfDz-FJQiFwIDAQAB";
    public static final String PRIVATE_JG = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCcu3Tt2qgUp2Ep2Sm0guRcVrO9hpBssoKHomGQAwqMda0Yie4WdqUogopPiF5LUMilMqaijZQ35hmEXiSfOyJGR2gA4K_joh0xkQHKqcDGQyfhegkQVpFV-AgX-7lxguU5cJkSvkE6XQBwqmf0MHIOWwfQb_JVzDyRBZgID9lAZzbPkeL2p-2dKv00qz7ByjGV0-LpJhEdwPr5RqEWYQfUM160f71ssglGaVnnjAlIALyOkZ4vc8k3aqeTDIEKNYFT3ZFlpJc_RD2C5Ogn1bfwAirIQOTlu0cjWcUbyNUaJuQEbAUmM3IN_7UvyeNClN2O63qQ72stSlzVUObkt9ILAgMBAAECggEAH4DadvjNciFWuWeqJ1tr9VeEF2NV4lNbQ1ITVDQ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_VqZJsrTuwqNMDNiHyJD8NseQAYBs8QY9L0sYOA6yF4Ib9r-I3i6uRaET95fIuam1fiSif_z3P1Sr3yvz5UYOkINJ_1jI749b4ZYrHxkX10";





    /**
     * 请求上游公共方法
     * @Param [reqBody 请求上游的业务参数]
     * @Return java.util.Map<java.lang.String, java.lang.String>
     */
    public static Map<String,String> getReqData(String reqBody,String url,boolean... isFormat){
        LogUtils.logInfo("请求报文:" + reqBody);
        LogUtils.logInfo("请求时间:" + DateUtils.getTime());
        HashMap<String, String> result = MapUtil.newHashMap();
        result.put("statusCode","300");
        result.put("message","请求失败,请重试!");
        JSONObject response = null;
        try {
            //通用请求参数
            Map<String, Object> req = new HashMap<>();
            req.put("orgNo", AGENT_NO_JG);
            req.put("reqId", System.currentTimeMillis());
            req.put("timestamp", DateUtils.getDateTime());
            req.put("version", "1.0");
            req.put("signType", "RSA");
            req.put("reqData", SecurityUtilParts.publicEncrypt(reqBody,PUBLIC_JG));
            String signStr = SecurityUtilParts.coverMap2String(JSONObject.parseObject(JSONObject.toJSONString(req)));
            req.put("sign",SecurityUtilParts.sign(signStr,PRIVATE_JG));
            String responseJson = HttpRequestUtil.sendPostJson(url, JSONObject.toJSONString(req));
            if( responseJson != null) {
                response = SecurityUtilParts.handlerResponse(responseJson, PUBLIC_JG, PRIVATE_JG);
                LogUtils.logInfo("响应报文:" + response);
                LogUtils.logInfo("响应时间:" + DateUtils.getTime());
                if (response == null) {
                    result.put("statusCode","300");
                    result.put("message","解密为空,签名解密失败");
                    return result;
                }

                if (isFormat.length > 0 && isFormat[0] == true ) {
                    return response.toJavaObject(Map.class);

                }

                String statusCode = response.getString("resultCode").equals("0000") ? "200" :"300";
                result.put("statusCode",statusCode);
                result.put("message",response.getString("resultMsg"));
                return result;

            }
        } catch (Exception e) {
            e.printStackTrace();
            result.put("statusCode","300");
            result.put("message","请求上游出现错误,请重试!");
            return result;
        }
            return result;
        }

}
